package com.aurora.oa.message.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.message.bean.MessageData;
import com.aurora.oa.message.bean.MessageUploadResponse;
import com.aurora.oa.message.bean.MyReceiveMessageBean;
import com.aurora.oa.message.bean.ResponseFileID;
import com.aurora.oa.message.view.MessageSendActivity;
import com.aurora.oa.message.view.MessageSendActivityView;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Administrator on 2017/03/20
*/

public class MessageSendActivityPresenterImpl implements MessageSendActivityPresenter{

    MessageSendActivityView messageSendActivityView;
    RequestHttpModel requestHttpModel;
    //上传成功服务器返回ID
    List<File> files;
    List<String> fileIDs;
    private MessageData messageData;
    public MessageSendActivityPresenterImpl(MessageSendActivityView messageSendActivityView) {
        this.messageSendActivityView = messageSendActivityView;
        requestHttpModel = new RequestHttpModelImpl();
    }

    @Override
    public void sendMessage(){
        //获取要发送的数据
        messageData= messageSendActivityView.getMessageData();
        //为空就不发送消息
        if(checkMessageNull()){
            return;
        }

        messageSendActivityView.showProgress();
        files = messageSendActivityView.getImageFiles();
        if(files.size()==0){
            //发送没图片的消息
            Map<String, String> params = new HashMap<>();
            params.put("name", messageData.name==null?"":messageData.name);
            params.put("content", messageData.content==null?"":messageData.content);
            params.put("userids", messageData.userids==null?"":messageData.userids);
            params.put("type", messageData.type==null?"":messageData.type);
            params.put("attachmentids", messageData.attachmentids==null?"":messageData.attachmentids);
            requestHttpModel.postForm(AuroraConstants.SEND_MESSAGE_URL,params,new OnRequestHttpDataListener(){

                @Override
                public void onRequestDataSuccess(String body) {

                    Gson gson = new Gson();
                    HttpResult  result = gson.fromJson(body, HttpResult.class);
                    messageSendActivityView.hideProgress();
                    if(result!=null&&result.success){
                        messageSendActivityView.onMessageUploadSuccess();
                    }else{
                        messageSendActivityView.onMessageUploadFail(result.msg);
                    }



                }

                @Override
                public void onRequestDataFail(String message) {
                    messageSendActivityView.hideProgress();
                    messageSendActivityView.onMessageUploadFail(message);
                }
            });
            return;
        }
        fileIDs = new ArrayList<>();
        for(int i = 0 ;i<files.size();i++){
            requestHttpModel.postFile(AuroraConstants.POST_FILE_URL, files.get(i), new OnRequestHttpDataListener() {
                @Override
                public void onRequestDataSuccess(String body) {
                    Gson gson = new Gson();
                    ResponseFileID idData = gson.fromJson(body,ResponseFileID.class);
                    if(idData!=null&&idData.isSuccess()){
                        fileIDs.add(idData.getMsg());
                        //图片上传完成
                        if(fileIDs.size()==files.size()){
                            //开始上传数据

                            messageData.attachmentids = StringUtils.getStringFromList(fileIDs);
                            Map<String, String> params = new HashMap<>();
                            params.put("name", messageData.name==null?"":messageData.name);
                            params.put("content", messageData.content==null?"":messageData.content);
                            params.put("userids", messageData.userids==null?"":messageData.userids);
                            params.put("type", messageData.type==null?"":messageData.type);
                            params.put("attachmentids", messageData.attachmentids==null?"":messageData.attachmentids);
                            requestHttpModel.postForm(AuroraConstants.SEND_MESSAGE_URL,params,new OnRequestHttpDataListener(){

                                @Override
                                public void onRequestDataSuccess(String body) {
                                    messageSendActivityView.hideProgress();
                                    messageSendActivityView.onMessageUploadSuccess();

                                }

                                @Override
                                public void onRequestDataFail(String message) {
                                    messageSendActivityView.hideProgress();
                                    messageSendActivityView.onMessageUploadFail(message);
                                }
                            });
                        }
                    }else{
                        messageSendActivityView.hideProgress();
                        messageSendActivityView.onFilesUploadFail(idData.getMsg());
                    }
                }

                @Override
                public void onRequestDataFail(String message) {
                    messageSendActivityView.hideProgress();
                    messageSendActivityView.onFilesUploadFail(message);

                }
            });
        }

    }

    //非空校验
    private boolean checkMessageNull() {

        if(StringUtils.isNullString(messageData.content)){
            messageSendActivityView.onMessageUploadFail("内容为空");
            return true;
        }

        if(StringUtils.isNullString(messageData.name)){
            messageSendActivityView.onMessageUploadFail("标题为空");
            return true;
        }
        if(StringUtils.isNullString(messageData.type)&&StringUtils.isNullString(messageData.userids)){
            messageSendActivityView.onMessageUploadFail("请选择消息发送对象");
            return true;
        }
            return false;
    }

}